package com.gzc.pm.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.gzc.pm.bean.EquconBean;

import cn.skyclass.business.WebConfig;
import cn.skyclass.ibatis.IBatisDao;

public class EquconDao {

	private IBatisDao dao = new IBatisDao();
	
	@SuppressWarnings("unchecked")
	public List<EquconBean> getInEquIds(String equIdsStr){
		List<EquconBean> origList = dao.getList("Equcon.getInEquIds", equIdsStr);
		//过滤检测项
		List<EquconBean> equconList = new ArrayList<EquconBean>();
		for(EquconBean equcon : origList){
			int equipmentId = equcon.getEquipmentId();
			String equconKey = equcon.getEquKeyV();
			if(containsEqucon(equipmentId, equconKey)){
				equconList.add(equcon);
			}
		}
		
		return equconList;
	}
	private boolean containsEqucon(int equipmentId, String equconKey){
		List<String> equconKeyList = getEquconKeyList(equipmentId);
		return equconKeyList.contains(equconKey);
	}
	private List<String> getEquconKeyList(int equipmentId){
		List<String> equconKeyStrList = new ArrayList<String>();
		
		String equconKeysStr = WebConfig.getInstance().getAppSettingValue("equipment"+equipmentId);
		if(StringUtils.isNotBlank(equconKeysStr)){
			String[] equconKeyStrArray = equconKeysStr.split(",");
			for(String equconKeyStr : equconKeyStrArray){
				equconKeyStrList.add(equconKeyStr);
			}
		}
		
		return equconKeyStrList;
	}
	
	@SuppressWarnings("unchecked")
	public List<EquconBean> getByProcId(int procId){
		return dao.getList("Equcon.getByProcId", procId);
	}
	
	@SuppressWarnings("unchecked")
	public List<EquconBean> getInEquIdsByTestItem(String equIdsStr){
		List<EquconBean> equconList = dao.getList("Equcon.getInEquIdsByTestItem", equIdsStr);
		return equconList;
	}
	
	@SuppressWarnings("unchecked")
	public List<EquconBean> getInEquIdsByFactor(int equId, String testItem){
		Map<String, Object> map = new HashMap<String, Object>(2);
		map.put("equId", equId);
		map.put("testItem", testItem);
		List<EquconBean> equconList = dao.getList("Equcon.getInEquIdsByFactor", map);
		return equconList;
	}
	
	@SuppressWarnings("unchecked")
	public List<EquconBean> getInEquIdsByFactorCheckBox(int equId, String testItem){
		Map<String, Object> map = new HashMap<String, Object>(2);
		map.put("equId", equId);
		map.put("testItem", testItem);
		List<EquconBean> equconList = dao.getList("Equcon.getInEquIdsByFactorCheckBox", map);
		return equconList;
	}
	
	public static void main(String[] args) {
		List<String> equconKeyList = new EquconDao().getEquconKeyList(23);
		boolean b1 = equconKeyList.contains("v1");
		boolean b2 = equconKeyList.contains("v122");
		System.out.println(b1);
		System.out.println(b2);
	}
}
